package com.klaus.ohMySpace.controller;

import com.klaus.ohMySpace.conf.Constants;
import com.klaus.ohMySpace.dao.UserDao;
import com.klaus.ohMySpace.entity.Note;
import com.klaus.ohMySpace.service.NoteService;
import com.klaus.ohMySpace.util.JsonUtil;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.klaus.ohMySpace.util.Params.*;

/**
 * Created by Mklaus on 15/8/16.
 */
@Controller
@RequestMapping("notes")
public class NoteController {

    @Resource
    private NoteService noteService;
    @Resource
    private UserDao userDao;

    @RequestMapping(method = RequestMethod.GET)
    public String notes_GET(Integer start,Integer size,Model model,HttpSession session){
        int userId = (Integer) session.getAttribute(Constants.USER_UID);

        List<Note> notes = noteService.getNotesByUserId(userId,start,size);
        model.addAttribute("notes",notes);
        return "/note/noteShowAll";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String notes_CREATE(String title,String content,Model model,HttpSession session){
        int userId = (Integer)session.getAttribute(Constants.USER_UID);
        Note note = new Note(title,content);
        note.setUser(userDao.get(userId));

        noteService.add(note);

        model.addAttribute("note",note);

        return "/note/noteShow";
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public String notes_DELETE(Integer noteId){
        Note note = noteService.get(noteId);
        note.setUser(null);
        System.out.println("note = " + note.getContent());
        noteService.delete(note);

        return "redirect:/notes";
    }

    @RequestMapping(value = "{noteId}",method = RequestMethod.GET)
    public String note_id(@PathVariable("noteId") Integer noteId, Model model){
        Note note = noteService.get(noteId);
        model.addAttribute("note",note);
        return "/note/noteShow";
    }


    //-------------------- for Json ------------------------
    @RequestMapping(value = "app_notes/{noteId}",method = RequestMethod.GET)
    public void app_notes(@PathVariable("noteId") Integer id,HttpServletResponse resp) throws IOException {
        Note note = noteService.get(id);
        JsonUtil.write(note, "note", resp);
    }

    @RequestMapping(value = "app_notes",method = RequestMethod.GET)
    public void app_notes_id(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        int     userId      = needInt(req, "userId");
        String  tag         = getString     (req,   "tag"           );
        Integer priority    = getInt        (req,   "priority"      );
        Date create_time    = getDate       (req,   "create_time"   );

        DetachedCriteria dc = DetachedCriteria.forClass(Note.class);
        dc.createCriteria("user").add(Restrictions.eq("id",userId));

        if (tag != null){
            dc.add(Restrictions.eq("tag",tag));
        }

        if (priority != null){
            dc.add(Restrictions.eq("priority",priority));
        }

        if (create_time != null) {
            dc.add(Restrictions.le("create_time",create_time));
        }

        List<Note> notes = noteService.get(dc);

        JsonUtil.write(notes, "notes", resp);
    }

    @RequestMapping(value = "app_notes",method = RequestMethod.DELETE)
    public void app_notes_DELETE(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        System.out.println("in = " + needInt(req,"id"));
        int id = needInt(req,"id");
        Note n = noteService.get(id);
        n.setUser(null);
        noteService.delete(n);
        JsonUtil.writeOK(resp);
    }

    @RequestMapping(value = "app_notes",method = RequestMethod.POST)
    public void app_notes_CREATE(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        Integer userId      = needInt       (req,   "userId"        );
        String  tag         = getString     (req,   "tag"           );
        Integer priority    = getInt        (req,   "priority"      );
        String  title       = getString     (req,   "title"         );
        String  content     = getString     (req,   "content"       );

        Note n = new Note(title,content);

        n.setUser(userDao.get(userId));


        if (tag != null){
            n.setTag(tag);
        }

        if (priority != null){
            n.setPriority(priority);
        }

        if (title != null){
            n.setTitle(title);
        }

        if (content != null){
            n.setContent(content);
        }

        noteService.add(n);

        JsonUtil.writeOK(resp);
    }

    @RequestMapping(value = "app_notes",method = RequestMethod.PUT)
    public void app_notes_UPDATE(HttpServletRequest req,HttpServletResponse resp){
        Integer id          = needInt       (req,   "id"            );
        String  tag         = getString     (req,   "tag"           );
        Integer priority    = getInt        (req,   "priority"      );
        String  title       = getString     (req,   "title"         );
        String  content     = getString     (req,   "content"       );

        Note n = noteService.get(id);

        if (tag != null){
            n.setTag(tag);
        }

        if (priority != null){
            n.setPriority(priority);
        }

        if (title != null){
            n.setTitle(title);
        }

        if (content != null){
            n.setContent(content);
        }

        noteService.update(n);

        JsonUtil.writeOK(resp);
    }
}
